/*
 * Copyright 2011 Michael Guo
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.uc.cs.guome.ir;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * Inverted Index Object
 * @author(name="Michael Guo",date="01/18/2011")
 * @version 0.1
 */
public class InvertedIndex implements Serializable {

	private static final long serialVersionUID = -187090313110175407L;
	private HashMap<String, PostingsList> invertedIndex;
	private HashSet<Integer> documentSet;
	
	public InvertedIndex() {
		this.invertedIndex = new HashMap<String, PostingsList>();
		this.documentSet = new HashSet<Integer>();
	}
	
	public void add(Document d){
		int id = d.getDocID();
		if (this.documentSet.contains(d))
			return;
		HashMap<String, TermFrequency> terms = d.getTerms();
		PostingsList postingsList = new PostingsList();
		Set<String> termSet = terms.keySet();
		Iterator<String> tir = termSet.iterator();
		while(tir.hasNext()) {
			String token = (String)tir.next();
			if (this.invertedIndex.containsKey(token)){
				postingsList = this.invertedIndex.get(token);
			}else{
				postingsList = new PostingsList();
			}
			TermFrequency tf = terms.get(token);
			postingsList.addNode(id, tf);
			this.invertedIndex.put(token, postingsList);
			this.documentSet.add(id);
		}
	}
	
	public void setInvertedIndex(HashMap<String, PostingsList> invertedIndex) {
		this.invertedIndex = invertedIndex;
	}
	public HashMap<String, PostingsList> getInvertedIndex() {
		return invertedIndex;
	}

	public void setDocumentSet(HashSet<Integer> documentSet) {
		this.documentSet = documentSet;
	}

	public HashSet<Integer> getDocumentSet() {
		return documentSet;
	}
	
	public void printInvertedIndex() {	
		SortedSet<String> ss = new TreeSet<String>(this.invertedIndex.keySet());
		Iterator<String> i = ss.iterator();
		while(i.hasNext()) {
			String s = i.next();
			s = s + "->" + this.invertedIndex.get(s).toString();
			System.out.println(s);
		}
	}
	
	public void saveInvertedIndex(String file) {
		try {
			PrintWriter pw = new PrintWriter(new FileWriter(file));
			SortedSet<String> ss = new TreeSet<String>(this.invertedIndex.keySet());
			Iterator<String> i = ss.iterator();
			
			while(i.hasNext()) {
				String s = i.next();
				s = s + " --> " + this.invertedIndex.get(s).toString();
				pw.println(s);
			}
		}catch(IOException e) {
			e.printStackTrace();
			return;
		}
	}
}
